<?php
/*********************************************************************************
* Filename: upload_image.php
* 
* This PHP page allows the user to upload a custom avatar for their team after
* creating a team.
*
*
* Author: Caleb Goff
* Date Created: 12/10/2009
**********************************************************************************/

//The common.php file contains 
include_once "common.php";

echo "$header_open";
echo "$css_create";
echo "$js_create_team";
echo "$header_close";

// Define some PHP code for uploading images

// Define maximum size for uploaded image
define ("MAX_SIZE","2"); 
$upload_status = "";
$image_name = "";

// This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
function getExtension($str) {
    $i = strrpos($str,".");
    if (!$i) { 
        return ""; 
    }
    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
    return $ext;
}

// This variable is used as a flag. The value is initialized with 0 (meaning no error found)  
// and it will be changed to 1 if an error occurs. If the error occures the file will not be uploaded.
$errors=0;

// Checks if the form has been submitted
if(isset($_POST['Submit'])) 
{
    // Header('Location:index.php');
    // Reads the name of the file the user submitted for uploading
 	$image=$_FILES['image']['name'];
 	
    // If it is not empty
 	if ($image) 
 	{
 	    // Get the original name of the file from the client's machine
 		$filename = stripslashes($_FILES['image']['name']);

 	    // Get the extension of the file in a lower case format
  		$extension = getExtension($filename);
 		$extension = strtolower($extension);
 	    
        // If it is not a known extension, we will suppose it is an error and will not  upload the file,  
	    // Otherwise we will do more tests
        if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
 		{
		    // Print error message
 			$upload_status = 'Unknown extension!';
 			$errors=5;
 		} else {
            // Get the size of the image in bytes
            // $_FILES['image']['tmp_name.txt'] is the temporary filename of the file
            // In which the uploaded file was stored on the server
            $size=filesize($_FILES['image']['tmp_name']);

            // Compare the size with the maxim size we defined and print error if bigger
            if ($size > MAX_SIZE*1024)
            {
	            $upload_status = 'Image exceeds the size limit!';
	            $errors=6;
            }

            // We will give an unique name, for example the time in if ($image_name != 0) {unix time format
            $image_name=time().'.'.$extension;
            // The new name will be containing the full path where will be stored (images folder)
            $newname="images/icons/teams/".$image_name;

            $team_email = $_GET['email'];
            $league_id  = $_GET['league'];
            $query = "UPDATE TEAMS SET AVATAR = '$image_name' WHERE OWNER_EMAIL = '$team_email' AND LEAGUE_ID = '$league_id'";
            do_query($query, $g_db_name, $g_username);

            // We verify if the image has been uploaded, and print error instead
            $copied = copy($_FILES['image']['tmp_name'], $newname);
            if (!$copied) 
            {
	            $upload_status = 'Copy unsuccessfull!';
	            $errors=7;
            }
        }
    }
}

//If no errors registered, print the success message
if(!$errors && $image_name != '') 
{
    $upload_status = "File Uploaded Successfully!";
}

?>

<body onload="postLoad();">

	<div id="wrapper">
		<div id="inner">
			<div id="masthead">
				<h1>
				</h1>
				<ul>
					<?php 
						echo"$g_nav_bar";
					?>
				</ul>
			</div>

			<div id="all_content">
				<h2>Upload Image</h2>
				Browse your computer to find the image you want to upload as your team's avatar. It must be 32x32 pixels and in JPEG, PNG, or GIF format.
				<h2>Upload Form</h2>
				<div id="info_box">
					<table>
						<tr>
                            <form name="newad" method="post" enctype="multipart/form-data"  action="">
							    <td class="input_title">Team Logo: </td>
							    <td class="input_value">
                                    <input type="file" name="image">
							    </td>
							    <input type='hidden' id='team_verify' value='empty'>
                                <?php
                                    echo "<input type='hidden' id='image_name' value='$image_name'>";
                                ?>
							    <td></td>
						    </tr>
                            <tr>
                                <td class="input_title"><?php echo $image_name ?></td>
                                <td class="input_value">
                                    <?php echo $upload_status ?>
                                </td>
                                <td>
                                    <?php 
                                        if ($image_name != 0) {
                                            echo "<img src='images/icons/teams/$image_name' height='32' width='32'>";
                                        }
                                    ?>
                                </td>
                            </tr>
					    </table>
				    </div>
				    <div class="create_team_header">
					        <input name="Submit" type="submit" value="Upload image">
                            <input type="button" value="Done" onclick="window.location='index.php';"/>
                    </form>
				</div>
				<h2></h2>
			</div>

			<div id="footer">
				<?php echo "$footer"; ?>
			</div>
		</div>
	</div>
</body>
</html>
